package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Donors;
import company.domain.Menu;
import company.domain.Rescuestations;
import company.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MenuController {
    @Resource
    MenuService menuService;

    /*
     * 分页查询
     * */
    @RequestMapping("/selectAllMenu/{page}")
    public R selectAllMenu(@PathVariable("page") int page,@RequestBody Menu menu){
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(menu.getTitle()!="" && !("".equals(menu.getTitle())),"title",menu.getTitle());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,2);
        List<Menu> list=this.menuService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }
    /*
     * 新增菜单
     * */
    @PostMapping("/AddMenu")
    public R AddMenu(@RequestBody Menu menu){
        this.menuService.save(menu);
        return new R();
    }

    /*
     * 菜单删除
     * */
    @RequestMapping("/DelMenu/{id}")
    public R DelMenu(@PathVariable("id") int id){
        boolean remove = this.menuService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据菜单id查询
     * */
    @PostMapping("/selectMenu")
    public R selectMenu(@RequestBody Menu menu){
        Menu byId = this.menuService.getById(menu.getId());
        return new R().add("byId",byId);
    }

    /*
     * 修改菜单
     * */
    @PostMapping("/uploadMenu")
    public R uploadMenu(@RequestBody Menu menu){
        this.menuService.updateById(menu);
        return new R();
    }

}
